package com.tuda.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.tuda.common.utils.DateUtils;
import com.tuda.common.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tuda.system.mapper.TudaOrderTransferMapper;
import com.tuda.system.domain.TudaOrderTransfer;
import com.tuda.system.service.ITudaOrderTransferService;
import com.tuda.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 货运中转Service业务层处理
 * 
 * @author tuda
 * @date 2023-09-22
 */
@Service
public class TudaOrderTransferServiceImpl implements ITudaOrderTransferService 
{
    @Autowired
    private TudaOrderTransferMapper tudaOrderTransferMapper;
    @Resource
    private TudaOrderServiceImpl orderService;

    /**
     * 查询货运中转
     * 
     * @param id 货运中转主键
     * @return 货运中转
     */
    @Override
    public TudaOrderTransfer selectTudaOrderTransferById(Long id)
    {
        return tudaOrderTransferMapper.selectTudaOrderTransferById(id);
    }

    /**
     * 查询货运中转列表
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 货运中转
     */
    @Override
    public List<TudaOrderTransfer> selectTudaOrderTransferList(TudaOrderTransfer tudaOrderTransfer)
    {
        return tudaOrderTransferMapper.selectTudaOrderTransferList(tudaOrderTransfer);
    }

    /**
     * 新增货运中转
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 结果
     */
    @Override
    public int insertTudaOrderTransfer(TudaOrderTransfer tudaOrderTransfer)
    {
        tudaOrderTransfer.setCreateTime(DateUtils.getNowDate());
        if(ToolUtil.isNotEmpty(tudaOrderTransfer.getAmount())){
            BigDecimal money = tudaOrderTransfer.getAmount().subtract(tudaOrderTransfer.getTransferFee());
            tudaOrderTransfer.setOwes(money.setScale(0, BigDecimal.ROUND_HALF_UP));
        }
        return tudaOrderTransferMapper.insertTudaOrderTransfer(tudaOrderTransfer);
    }

    /**
     * 修改货运中转
     * 
     * @param tudaOrderTransfer 货运中转
     * @return 结果
     */
    @Override
    public int updateTudaOrderTransfer(TudaOrderTransfer tudaOrderTransfer)
    {
        tudaOrderTransfer.setUpdateTime(DateUtils.getNowDate());
        if(ToolUtil.isNotEmpty(tudaOrderTransfer.getAmount())){
            BigDecimal money = tudaOrderTransfer.getAmount().subtract(tudaOrderTransfer.getTransferFee());
            tudaOrderTransfer.setOwes(money.setScale(0, BigDecimal.ROUND_HALF_UP));
        }
        int rows = tudaOrderTransferMapper.updateTudaOrderTransfer(tudaOrderTransfer);
//           if(rows > 0){
//              orderService.statistics(tudaOrderTransfer.getOrderId());
//           }
        return rows;
    }

    /**
     * 批量删除货运中转
     * 
     * @param ids 需要删除的货运中转主键
     * @return 结果
     */
    @Override
    public int deleteTudaOrderTransferByIds(String ids)
    {
        return tudaOrderTransferMapper.deleteTudaOrderTransferByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除货运中转信息
     * 
     * @param id 货运中转主键
     * @return 结果
     */
    @Override
    public int deleteTudaOrderTransferById(Long id)
    {
        return tudaOrderTransferMapper.deleteTudaOrderTransferById(id);
    }

    @Override
    public int updateOrderTransfer(Long id, String tName, String tValue) {
        tName = tName.replaceAll("[0-9]", "");
        if(tName.equals("amount") || tName.equals("transfer_fee")){
            TudaOrderTransfer orderTransfer = selectTudaOrderTransferById(id);
            if(ToolUtil.isNotEmpty(orderTransfer)){
                BigDecimal money = new BigDecimal(0.00);
                if(tName.equals("amount")){
                    money = BigDecimal.valueOf(Long.parseLong(tValue)).subtract(orderTransfer.getTransferFee());
                    orderTransfer.setAmount(BigDecimal.valueOf(Long.parseLong(tValue)));
                }
                if(tName.equals("transfer_fee")){
                    money = orderTransfer.getAmount().subtract(BigDecimal.valueOf(Long.parseLong(tValue)));
                    orderTransfer.setTransferFee(BigDecimal.valueOf(Long.parseLong(tValue)));
                }
                orderTransfer.setOwes(money.setScale(0, BigDecimal.ROUND_HALF_UP));
                return updateTudaOrderTransfer(orderTransfer);
            }
        }
        if(!tName.equals("amount") && !tName.equals("transfer_fee")){
            return tudaOrderTransferMapper.updateOrderFee(id, tName, tValue);
        }
        return 0;
    }
}
